package com.lsa.skienna.challenge.chap4.the110401_VitosFamily;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;

public class Main implements Runnable{
	public static void main(String[] args) throws Exception {
		new Main().run();
	}
	
	@Override
	public void run() {
		TaskReader taskReader = new TaskReader(System.in);
		try {
			Task task = null;
			while((task = taskReader.nextTask()) != null){
				System.out.println(optimalLocationSumOfDistances(task));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	static int optimalLocationSumOfDistances(Task task) {
		int[] kindredLocations = task.kindredLocations;
		Arrays.sort(kindredLocations);
		
		int locationCount = 0;
		int prevLocation = -1;
		for (int i = 0; i < kindredLocations.length; ++i) {
			if (kindredLocations[i] != prevLocation) {
				locationCount++;
			}
			prevLocation = kindredLocations[i];
		}
		
		prevLocation = -1;
		int j = -1;
		
		int[] locations = new int[locationCount];
		int[] kindredCount = new int[locationCount];

		int[] partialSumLeft  = new int[locationCount];
		int[] partialSumRight  = new int[locationCount];

		for (int i = 0; i < kindredLocations.length; ++i) {
			if (prevLocation == kindredLocations[i]) {
				kindredCount[j]++;
			}
			else {
				prevLocation = kindredLocations[i];
				++j;
				locations[j] = kindredLocations[i];
				kindredCount[j] = 1;
			}
		}
		
		int pointCountLeft = 0;
		int pointCountRight = 0;
		
		for (int i = 0; i < locations.length; ++i) {
			if (i > 0) {
				pointCountLeft = pointCountLeft + kindredCount[i - 1];
				partialSumLeft[i] = partialSumLeft[i - 1] + pointCountLeft*Math.abs(locations[i]-locations[i-1]);
			}
		}
		
		for (int i = locations.length - 1; i > -1; --i) {
			if (i < locations.length - 1) {
				pointCountRight = pointCountRight + kindredCount[i + 1];
				partialSumRight[i] = partialSumRight[i + 1] + pointCountRight*Math.abs(locations[i]-locations[i+1]);
			}
		}
		
		int minTotalSum = Integer.MAX_VALUE;
		for (int i = 0; i < locations.length; ++i) {
			int partialSum = partialSumLeft[i] + partialSumRight[i];
			if (minTotalSum > partialSum) {
				minTotalSum = partialSum;
			}
		}
		
		return minTotalSum;
	}
	
	static int optimalLocationSumOfDistancesByMedian(Task task) {
		int[] kindredLocations = task.kindredLocations;
		Arrays.sort(kindredLocations);
		
		int minTotalSum = 0;
		int count = kindredLocations.length;
		if (count % 2 == 1) {
			minTotalSum = weight(kindredLocations[count / 2], kindredLocations);
		}
		else {
			int weightMid1;
			int weightMid2;
			if (count == 2) {
				weightMid1 = weight(kindredLocations[0], kindredLocations);
				weightMid2 = weight(kindredLocations[1], kindredLocations);
			}
			else {
				weightMid1 = weight(kindredLocations[count / 2], kindredLocations);
				weightMid2 = weight(kindredLocations[count / 2 + 1], kindredLocations);
			}
			minTotalSum = Math.min(weightMid1, weightMid2);
		}
		
		return minTotalSum;
	}
	
	static int weight(int addr, int[] streetNums) {
		int total = 0;
		for (int j = 0; j < streetNums.length; j++) {
			total += Math.abs(streetNums[j] - addr);
		}
		return total;
	}
	
	static class TaskReader{
		private BufferedReader reader;
		private boolean isFirst;
		private int caseNumber;

		TaskReader(InputStream stream) {
			this.reader = new BufferedReader(new InputStreamReader(stream));
			this.isFirst = true;
		}
		
		Task nextTask() throws IOException {
			if (isFirst) {
				String line = reader.readLine();
				if (line == null){
					return null;
				}
				caseNumber = Integer.parseInt(line.trim());
				isFirst = false;
			}
			
			Task task = null;
			
			if (caseNumber-- > 0) {
				String line = reader.readLine();
				if (line == null){
					return null;
				}
				Scanner scanner = new Scanner(line);
				
				int kindredsCount = scanner.nextInt();
				task = new Task(new int[kindredsCount]);
				
				for (int i = 0; i < kindredsCount; i++) {
					task.kindredLocations[i] = scanner.nextInt();
				}
			}
			
			return task;
		}
	}
	static class Task {
		final int[] kindredLocations;

		public Task(int[] kindredLocations) {
			this.kindredLocations = kindredLocations;
		}
	}
}
